﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using nRoute.Behaviors.Interactivity;
using Windows.Foundation;
using Windows.UI.Input;
using Windows.UI.Xaml;

namespace SimpleShell.Behaviors
{
    public class SlideBackGestureTrigger
        : TriggerBase<FrameworkElement>
    {
        private GestureRecognizer _recognizer;
        private Point _startPoint;

        public SlideBackGestureTrigger() { }

        protected override void OnAttached()
        {
            base.OnAttached();

            _recognizer = new GestureRecognizer();
            _recognizer.GestureSettings = GestureSettings.CrossSlide;
            _recognizer.CrossSlideHorizontally = true;
            _recognizer.CrossSliding += Recognizer_CrossSliding;

            this.AssociatedObject.PointerPressed += AssociatedObject_PointerPressed;
            this.AssociatedObject.PointerMoved += AssociatedObject_PointerMoved;
            this.AssociatedObject.PointerReleased += AssociatedObject_PointerReleased;            
        }

        protected override void OnDetaching()
        {
            this.AssociatedObject.PointerPressed -= AssociatedObject_PointerPressed;
            this.AssociatedObject.PointerMoved -= AssociatedObject_PointerMoved;
            this.AssociatedObject.PointerReleased -= AssociatedObject_PointerReleased;
            
            _recognizer.CrossSliding -= Recognizer_CrossSliding;
            _recognizer = null;

            base.OnDetaching(); 
        }

#region Handlers

        private void AssociatedObject_PointerReleased(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            var _points = e.GetIntermediatePoints(this.AssociatedObject);
            if (_points != null && _points.Count > 0)
            {
                _recognizer.ProcessUpEvent(_points[0]);
                e.Handled = true;
            }
        }

        private void AssociatedObject_PointerMoved(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            var _points = e.GetIntermediatePoints(this.AssociatedObject);

            if (_points != null && _points.Count > 0)
            {
                _recognizer.ProcessMoveEvents(_points);
                e.Handled = true;
            }
        }

        void AssociatedObject_PointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            var _points = e.GetIntermediatePoints(this.AssociatedObject);
            if (_points != null && _points.Count > 0)
            {
                _recognizer.ProcessDownEvent(_points[0]);
                _startPoint = _points[0].Position;
                e.Handled = true;
            }
        }

        void Recognizer_CrossSliding(GestureRecognizer sender, CrossSlidingEventArgs args)
        {
            if (args.CrossSlidingState == CrossSlidingState.Completed) // && args.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch
            {
                // direction check
                if (_startPoint.X < args.Position.X) base.InvokeActions(null);
            }
        }

#endregion

    }
}
